#!/bin/bash
################################################################################
# Script to plot a CLEANPP spectra file                        
################################################################################
# Bmort --- bmort@astro.ox.ac.uk ---                     last update: 02 OCT 07
################################################################################
# COMMAND LINE INPUTS:
#
# 1. spectra directory (location of the cleanSpect.dat file to be plotted)
# 2. plot title (main title of the plot)
# 3. the type of the plot (CLEAN spect, dirty spect. etc...)
# 4. plotid used if more than 1 plot of the same type is required
################################################################################

###################################################################
#HARDCODED INPUTS
###################################################################
XMIN=0.06
XMAX=0.09
XTICS=0.01   # x tick increment
MXTICS=4    # number of sub tics between x tics
MYTICS=4    # number of sub tics between y tics
COLX=1      # x column data
COLY=2      # y column data (mag data)
COLY2=5     # y2 column data (phase data)
###################################################################
#XLABEL="Period (days)"
XLABEL="Frequency (Hz)"
###################################################################
YLABEL="Modulus"
YLABEL2="Phase ({/Symbol p})"
###################################################################
echo ""
echo "################ PLOT SPECTRA ####################"

#ARG CHECK
if [ $# -ne 4 ]
    then                                                                        
    echo ""
    echo "ERROR: missing form command line arguments. ($# out of 4 found)"
    echo "USAGE: $> sh PLOTspectra.sh [DIR] [TITLE] [TYPE] [PLOTID]"     
    echo ""
    echo ""
    exit 1                                                                      
fi  

##COMMAND LINE VARIABLES 
DIR=$1
TITLE=$2
TYPE=$3
PLOTID=$4


# PROCESS PLOT TYPE
if [ $TYPE = "C" ]; then
    SPECT_NAME="clean.dat"
    PLOT_NAME="clean"
elif [ $TYPE = "CR" ]; then
    SPECT_NAME="clean-residual.dat"
    PLOT_NAME="clean-residual"
elif [ $TYPE = "D" ]; then
    SPECT_NAME="dirty.dat"
    PLOT_NAME="dirty"
elif [ $TYPE = "R" ]; then
    SPECT_NAME="residual.dat"
    PLOT_NAME="resid"
elif [ $TYPE = "W" ]; then
    SPECT_NAME="window.dat"
    PLOT_NAME="window"
elif [ $TYPE = "CC" ]; then    
    SPECT_NAME="component.dat"
    PLOT_NAME="compoents"
elif [ $TYPE = "CB" ]; then
    SPECT_NAME="beam.dat"
    PLOT_NAME="beam"
fi

if [ -z $SPECT_NAME ]; then
    echo "TYPE UNRECOGNISED!"
    echo "types are: C CR D R W CC CB"
    exit 2
fi

PLOT_NAME="PLOT_"$PLOT_NAME"-$PLOTID"
PLOT_SCRIPT=${DIR}"/${PLOT_NAME}.plot"
PLOT_FILE=${DIR}"/${PLOT_NAME}.ps"
SPECT_FILE=${DIR}"/${SPECT_NAME}"

#check to see if the spectfile input exists
if [ -f $SPECT_FILE ]
then
    echo "" # the file exisits so we are ok
else
  echo "SPECTRA FILE DOES NOT EXIST. ${SPECT_FILE} exiting!\n"
  exit
fi

echo "=================================================="
echo "$0 [DIR] [TITLE] [PLOT NAME] [TYPE]"
echo "=================================================="
echo "number of args found = $#"
echo "DIR            = $DIR"
echo "PLOT TYPE      = $TYPE ($SPECT_NAME)"
echo "PLOTID         = $PLOTID" 
echo "PLOT NAME ROOT = $PLOT_NAME"
echo "PLOT SCRIPT    = $PLOT_SCRIPT"
echo "PLOT FILE      = $PLOT_FILE"
echo "TITLE          = $TITLE"
echo "=================================================="
echo "** HARDCODED VALUES ** CHECK THESE **"
echo "XMIN       = ${XMIN}"
echo "XMAX       = ${XMAX}"
echo "XTICS      = ${XTICS}"
echo "MXTICS     = ${MXTICS}"
echo "MYTICS     = ${MYTICS}"
echo "COLX       = ${COLX}"
echo "COLY       = ${COLY}"
echo "COLY2      = ${COLY2}"
echo "=================================================="
echo ""


plot_file_template () { # 1(spect file) 2(title) 3(ps plot file) 
    echo "##################################################################"
    echo "# Gnuplot script file"
    echo "#-----------------------------------------------------------------"
    echo "# to run the script either enter gnuplot and type:"
    echo "#     gnuplot> load ''"
    echo "# or run form the command line with "
    echo "#     $ > gunplot /path/to/script....plot"
    echo "#-----------------------------------------------------------------"
    echo "#                                                  bmort - 02OCT07"
    echo "##################################################################"
    echo
    echo "!echo \"--------------------------\""
    echo "!echo \"runing script from ...\""
    echo "!pwd"
    echo "!echo \"--------------------------\""
    echo "reset"
    echo "set terminal postscript landscape enhanced color dashed lw 1 \"Helvetica\" 11"
    echo "set output \"TMPplot.ps\""
    echo ""
    echo "set xrange [ ${XMIN} : ${XMAX} ]"
    echo "unset key"
    echo "set xtics ${XTICS}"
    echo "set mxtics ${MXTICS}"
    echo "set mytics ${MYTICS}"
#    echo "set grid xtics ytics mxtics"
    echo "set lmargin 14"
    echo "set rmargin 4"
    echo "#================================================"
    echo "set multiplot"
    echo "set ylabel \"${YLABEL}\""
    echo "set size 1.0,0.6"
    echo "set xlabel \"${XLABEL}\""
    echo "set origin 0.0,0.0"
    echo "set bmargin 5"
    echo "set tmargin 0"
    echo "#------------------------------------------------"
    echo "plot \"$1\" u ${COLX}:${COLY} w l lw 0.5"
    echo "#------------------------------------------------"
    echo "unset label"
    echo "set title \"$2\" font \"Helvetica,16\""
    echo "set format y \"%1.3g\""
    echo "set yrange [-1.1 : 1.1]"
    echo "set ytics 0.5"
    echo "set mytics 4"
    echo "unset grid"
 #   echo "set grid xtics ytics mxtics"
    echo "set ylabel \"${YLABEL2}\""
    echo "set size 1.0,0.3"
    echo "set origin 0.0,0.62"
    echo "set bmargin 0"
    echo "set tmargin 2"
    echo "set format x \"\""
    echo "set xlabel \"\""
    echo "set pointsize 0.2"
    echo "#------------------------------------------------"
    echo "plot \"$1\" u ${COLX}:(\$${COLY2}/pi) w p"
    echo "#------------------------------------------------"
    echo "unset multiplot"
    echo "#================================================"
    echo "set terminal x11"
    echo "!mv TMPplot.ps $3"
    echo ""
}

make_plot_file () {
    echo "make plot file ..."
    echo "1 data file    = ${1}"
    echo "2 graph title  = ${2}"
    echo "3 plot file    = ${3}"
    echo "4 plot script  = ${4}"
    plot_file_template $1 "${2}" $3 > $4
}

make_clean_spec() {
    make_plot_file $1 "${2}" $3 $4
    echo ""
    echo "==> Loading gnuplot script file <=="
    gnuplot $4
}



make_clean_spec ${SPECT_FILE} "${TITLE}" ${PLOT_FILE} ${PLOT_SCRIPT}
echo ""
echo "-- DONE"
echo "========================================================"
echo "gv ${PLOT_FILE} &"
echo "emacs -nw ${PLOT_SCRIPT}"
echo "cp ${PLOT_FILE} "
echo "========================================================"
